home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 8715 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.1 KB

  1. Path: newshost.lanl.gov!tanmoy
  2. From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Question on stingizing
  5. Date: 04 Mar 1996 00:24:12 GMT
  6. Organization: Los Alamos National Laboratory
  7. Message-ID: <TANMOY.96Mar3172412@qcd.lanl.gov>
  8. References: <4hda43$f15@skivs.ski.org>
  9. NNTP-Posting-Host: qcd.lanl.gov
  10. Mime-Version: 1.0
  11. Content-Type: text
  12. In-reply-to: gt@ns.oon.or.jp's message of Sun, 03 Mar 1996 23:26:38 GMT
  13.  
  14. In article <4hda43$f15@skivs.ski.org>
  15. gt@ns.oon.or.jp (Gemini Thunder) writes:
  16.  
  17. GT:   I have looked over this problem, but can not figure out the why of
  18. GT: it.  I don't have a copy of the Standard, but I have read the FAQ 
  19. GT: (11.17), but it does not give the why of it either....
  20. GT:   Why is this necessary:
  21. GT: 
  22. GT: #define A Hello
  23. GT: #define s(x) #x
  24. GT: #define ss(x) s(x)  /* why this circumvention for #defined ?? */
  25.  
  26. Roughly because # prohibits expansion of its parameter: macro
  27. arguments are otherwise (i.e. unless # and ## overrides this rule)
  28. expanded completely before substitution. i.e. with the # operator you
  29. get the stringized version of the unexpanded argument to the macro.
  30.  
  31. ss(A) now expands in the following stages:
  32.  
  33. ss(A) has argument A which is first expanded
  34.   A --> Hello
  35. and then replaced into the pattern which is then expanded:
  36. ss(A) --> s(Hello) --> "Hello"
  37.  
  38. Whereas
  39.  
  40. s(A) --> "A" 
  41.  
  42. because argument A is not expanded before substitution.
  43.  
  44. To take a more complicated example, given ss(ss(s(A))),
  45.  
  46.   s(A) --> "A"
  47.  ss(s(A)) --> s("A") --> "\"A\""
  48. ss(ss(s(A))) --> s("\"A\"") --> "\"\\\"A\\\"\""
  49.  
  50. If you understand the above, you understand macro expansion better
  51. than most beginners do.
  52.  
  53. Cheers
  54. Tanmoy
  55. --
  56. tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
  57. Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
  58. Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
  59. <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
  60. internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
  61. fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]
  62.